package com.mybed.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybed.crm.common.enums.HttpCodeEnum;
import com.mybed.crm.mapper.BusinessMapper;
import com.mybed.crm.pojo.dto.PageContractQueryDto;

import com.mybed.crm.pojo.pojo.Business;
import com.mybed.crm.pojo.pojo.Contract;
import com.mybed.crm.mapper.ContractMapper;
import com.mybed.crm.pojo.vo.ContractVo;
import com.mybed.crm.result.PageResult;
import com.mybed.crm.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 合同 服务实现类
 * </p>
 *
 * @author kt
 * @since 2024-10-26
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService {

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private ContractMapper contractMapper;
    @Override
    public PageResult<ContractVo> pageContractQuery(PageContractQueryDto pageContractQueryDto) {

        String contractNo = pageContractQueryDto.getContractNo();
        String name = pageContractQueryDto.getName();
        String phone = pageContractQueryDto.getPhone();
        String subject = pageContractQueryDto.getSubject();
        String courseId = pageContractQueryDto.getCourseId();
        String endCreateTime = pageContractQueryDto.getParams().get("endCreateTime");
        String beginCreateTime = pageContractQueryDto.getParams().get("beginCreateTime");
        Integer pageNum = pageContractQueryDto.getPageNum();
        Integer pageSize = pageContractQueryDto.getPageSize();

        Page<Contract> page = new Page<>(pageNum, pageSize);
        Page<Contract> p = lambdaQuery().eq(contractNo != null && !contractNo.isEmpty(), Contract::getContractNo, contractNo)
                .like(name != null, Contract::getName, name)
                .like(phone != null, Contract::getPhone, phone)
                .eq(subject != null, Contract::getSubject, subject)
                .eq(courseId != null, Contract::getCourseId, courseId)
                .between(StringUtils.hasLength(beginCreateTime) && StringUtils.hasLength(endCreateTime) , Contract::getCreateTime, beginCreateTime, endCreateTime)
                .page(page);

        //封装vo结果
        List<Contract> records = p.getRecords();
        List<ContractVo> contractVoList =new ArrayList<>();
        records.forEach(contract ->{
            ContractVo contractVo = new ContractVo();
            BeanUtil.copyProperties(contract,contractVo);
            contractVoList.add(contractVo);
        });
        PageResult<ContractVo> result = new PageResult<>(p.getTotal(), contractVoList,null);
        result.setCode(HttpCodeEnum.SUCCESS.getCode());
        result.setMsg(HttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 商机转为客户
     * @param id
     * @param contract
     */
    @Override
    @Transactional
    public void addContracts(Long id, Contract contract) {
        Business business = businessMapper.selectById(id);
        contract.setCourseId(business.getCourseId());
        contract.setActivityId(Long.valueOf(business.getActivityId()));
//      contract.setContractOrder(Float.valueOf(business.getSalary()));
//      contract.setCoursePrice(Float.valueOf(business.getSalary()));
        contract.setDiscountType(String.valueOf(business.getLevel()));
        contract.setActivityName(business.getActivityName());
        contract.setCourseName(business.getSubject());
        contract.setName(business.getName());
        contract.setPhone(business.getPhone());
        contract.setSubject(business.getSubject());
        contract.setChannel(String.valueOf(business.getChannel()));
        contract.setStatus(String.valueOf(business.getStatus()));
        contract.setCreateTime(LocalDateTime.now());
        contract.setBusinessId(business.getId());
        business.setStatus(5);
        contractMapper.insert(contract);
        businessMapper.updateById(business);
    }
}
